package com.zxw.flow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxw.common.web.annotation.InitInsertInfo;
import com.zxw.common.web.annotation.InitSelectInfo;
import com.zxw.common.web.util.JwtUtils;
import com.zxw.flow.domain.entity.FlowNodes;
import com.zxw.flow.domain.entity.FlowNodes;
import com.zxw.flow.domain.entity.FlowType;
import com.zxw.flow.domain.vo.FlowNodesPageVO;
import com.zxw.flow.domain.vo.FlowNodesVO;
import com.zxw.flow.domain.vo.FlowNodesVO;
import com.zxw.flow.mapper.FlowNodesMapper;
import com.zxw.flow.service.FlowNodesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-07-04
 */
@Service
@RequiredArgsConstructor
public class FlowNodesServiceImpl extends ServiceImpl<FlowNodesMapper, FlowNodes> implements FlowNodesService {

    private final FlowNodesMapper flowNodesMapper;

    @Override
    @InitSelectInfo
    public IPage<FlowNodes> getPage(FlowNodesPageVO pageVO) {
        Page<FlowNodes> flowNodesPage = new Page<>(pageVO.getPageNum(), pageVO.getPageSize());
        LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<FlowNodes>()
                .eq(FlowNodes::getCompanyId, pageVO.getCompanyId())
                .eq(FlowNodes::getFlowBasicInfoId, pageVO.getFlowBasicInfoId())
                .eq(null != pageVO.getNodeName(), FlowNodes::getNodeName, pageVO.getNodeName())
                .eq(null != pageVO.getShape(), FlowNodes::getShape, pageVO.getShape());
        Page<FlowNodes> pageData = flowNodesMapper.selectPage(flowNodesPage, wrapper);
        return pageData;
    }

    @Override
    @InitInsertInfo
    public FlowNodesVO add(FlowNodesVO flowNodesVO) {
        flowNodesMapper.insert(flowNodesVO);
        return flowNodesVO;
    }

    @Override
    public FlowNodes getFlowNodesVOById(Long id) {
        Long companyId = JwtUtils.getCompanyId();
        FlowNodes flowNodes = flowNodesMapper.selectOne(new LambdaQueryWrapper<FlowNodes>()
                .eq(FlowNodes::getId, id)
                .eq(FlowNodes::getCompanyId, companyId)
        );
        return flowNodes;
    }

    @Override
    public void updateBase(FlowNodesVO flowTypeVO) {
        flowNodesMapper.update(flowTypeVO, new LambdaUpdateWrapper<FlowNodes>()
                .eq(FlowNodes::getId, flowTypeVO.getId())
                .eq(FlowNodes::getCompanyId, flowTypeVO.getCompanyId())
        );
    }

    @Override
    public void delete(String flowNodesIds) {
        Long companyId = JwtUtils.getCompanyId();
        flowNodesMapper.delete(new LambdaQueryWrapper<FlowNodes>()
                .eq(FlowNodes::getCompanyId, companyId)
                .in(FlowNodes::getId, flowNodesIds)
        );
    }
}
